package com.lyz.stringdemo;

import java.util.Random;
import java.util.Scanner;

/**
 * @ClassName StringExec1
 * @Description 登录功能，验证码功能
 * @Author 2214361854
 * @Date 2022/2/15 23:26
 * @Version 1.0
 */
public class StringExec1 {
    public static void main(String[] args) {
        String okLoginName = "admin";
        String okPassword = "123456";
        dlgn(okLoginName,okPassword);
    }

    /**
     * 生成验证码
     * @param index
     * @return
     */
    public static String scyzm(int index){
        String datas = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        Random random = new Random();
        String code ="";
        for (int i = 0; i < index; i++) {
            //随机取字符串索引
            int in = random.nextInt(datas.length());
            char c = datas.charAt(in);
            code += c;
        }
        return code;
    }

    /**
     * 登录功能
     * @param okLoginName
     * @param okPassword
     */
    public static void dlgn(String okLoginName,String okPassword){
        Scanner scanner = new Scanner(System.in);
        String scyzm = scyzm(5);
        System.out.println(scyzm);
        int numyzm = 3;
        for (int i = 1; i <= 3; i++) {
            System.out.println("请您输入验证码：");
            if (!scyzm.equals(scanner.next())){
                numyzm--;
                System.out.println("验证码错误,还有"+(numyzm)+"次机会");
            }
        }
        //三次机会
        int number = 3;
        for (int i = 1; i <= number&&numyzm>0; i++) {
            System.out.println("请您输入登录名称：");
            String LoginName = scanner.next();
            if(okLoginName.equals(LoginName)){
                System.out.println("请您输入登录密码：");
                String Password = scanner.next();
                if(okPassword.equals(Password)){
                    System.out.println("登录成功");
                    break;
                }else {
                    System.out.println("密码错误，还有"+(number-i)+"次机会");
                }
            }else {
                System.out.println("登录名错误，还有"+(number-i)+"次机会");
            }
        }
    }
}
